<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Brand;
use App\Models\Area;

class HomeController extends Controller
{
    //banner
    public function banner()
    {
        //banner
        $banner = DB::table('articles')->where('type', '1')->select('id', 'name', 'picture', 'content')->limit(3)->get();

        //咨询
        $consult = DB::table('articles')->where('type', '2')->select('id', 'name', 'picture', 'content')->limit(3)->get();

        $return['banner'] = $banner;
        $return['consult'] = $consult;

        return ['code' => 1, 'message' => '请求成功', 'data' => $return];
    }

    //报价
    public function SheetList(Request $request)
    {
        $sheet = DB::table('quotation_sheets')->where('is_show', '1')->get();
        foreach ($sheet as $k => $v) {
            $sheet[$k]->content = (!empty($v->content)) ? explode(',', $v->content) : [];
            $sheet[$k]->background_image = (!empty($v->background_image)) ? $v->background_image : '';
        }

        return ['code' => 1, 'message' => '请求成功', 'data' => $sheet];
    }

    //客服电话
    public function service()
    {
        $service = DB::table('configurations')->where('en_name', 'service_phone')->value('parameter');

        return ['code' => 1, 'message' => '请求成功', 'data' => $service];
    }

    //添加申请订单记录
    public function addOrder(Request $request)
    {
        //获取参数
        $u_id = $request->u_id;
        $information = $request->post('information');
        $amount = $request->post('amount');
        $note = $request->post('note');
        $picture = $request->post('picture');

        if(empty($information) || empty($amount)) {
            return ['code' => 0, 'message' => '缺少必要参数', 'data' => ''];
        }

        $user = DB::table('users')->where('id', $u_id)->first();
        if($user->is_partner == '0' || $user->is_partner == '1') {
            return ['code' => 0, 'message' => '用户未申请成为合作商或正在审核，不能发布订单', 'data' => ''];
        }

        if(empty($user->alipay_account) || empty($user->alipay_name) || empty($user->bank_name) || empty($user->bank_number) || empty($user->bank_type) || empty($user->opening_bank)) {
            return ['code' => 0, 'message' => '用户收款信息为完善，请先完善收款信息', 'data' => ''];
        }

        //插入数据
        $inst['u_id'] = $u_id;
        $inst['order_no'] = orderNo();
        $inst['information'] = $information;
        $inst['amount'] = $amount;
        $inst['note'] = $note;
        $inst['picture'] = (empty($picture)) ? '' : $picture;
        $inst['create_at'] = date('Y-m-d H:i:s', time());
        $res = DB::table('orders')->insertGetId($inst);
        if (!$res) {
            return ['code' => 0, 'message' => '请求失败', 'data' => ''];
        }

        DB::table('users')->where('id', $u_id)->increment('issue_orders');

        return ['code' => 1, 'message' => '发布成功', 'data' => $res];
    }

    //收件地址数据
    public function address()
    {
        $info = DB::table('configurations')->where('en_name', 'consignee')
            ->orWhere('en_name', 'contact_phone')->orWhere('en_name', 'shipping_address')
            ->pluck('parameter', 'en_name');
        return ['code' => 1, 'message' => '请求成功', 'data' => $info];
    }

    //售后查询
    public function AfterQuery(Request $request)
    {
        //获取参数
        $imei = $request->post('imei');

        $info = DB::table('after_information')->where('imei', $imei)->select('after_information.phone_model', 'after_information.deadline as warranty_time', 'users.name')
                    ->leftJoin('users', 'users.id', '=', 'after_information.provider_id')
                    ->first();

        if(empty($info)) {
            return ['code' => 0, 'message' => 'IMEI码不存在', 'data' => ''];
        }

        return ['code' => 1, 'message' => '请求成功', 'data' => $info];
    }

    //成为合作商
    public function partner(Request $request)
    {
        //获取参数
        $u_id = $request->u_id;
        $name = $request->post('name');
        $phone = $request->post('phone');
        $address = $request->post('address');
        $identity = $request->post('identity');
        $id_card = $request->post('id_card');
        $id_card_positive = $request->post('id_card_positive');
        $id_card_contrary = $request->post('id_card_contrary');

        //判断状态
        if(empty($name) || empty($phone) || empty($address) || empty($identity)) {
            return ['code' => 0, 'message' => '缺少必要参数', 'data' => ''];
        }

        $user = DB::table('users')->where('id', $u_id)->first();
        if($user->is_partner != 0) {
            return ['code' => 0, 'message' => '用户已提交合作商申请', 'data' => ''];
        }
        if($user->identity == 1 || $user->identity == 2 || $user->identity == 3) {
            return ['code' => 0, 'message' => '用户已成为合作商', 'data' => ''];
        }
//        if(empty($user->alipay_account) || empty($user->alipay_name) || empty($user->bank_name) || empty($user->bank_number) || empty($user->bank_type) || empty($user->opening_bank)) {
//            return ['code' => 0, '用户收款信息未完善，请先完善收款信息', 'data' => ''];
//        }


        //插入数据
        $update['name'] = $name;
        $update['partner_name'] = $name;
        $update['partner_phone'] = $phone;
        $update['partner_address'] = $address;
        $update['identity'] = $identity;
        $update['id_card'] = $id_card;
        $update['id_card_positive'] = (empty($id_card_positive)) ? '' : $id_card_positive;
        $update['id_card_contrary'] = (empty($id_card_contrary)) ? '' : $id_card_contrary;
        $update['is_partner'] = 1;
        $res = DB::table('users')->where('id', $u_id)->update($update);
        if(!$res) {
            return ['code' => 0, 'message' => '请求失败', 'data' => ''];
        }
        return ['code' => 1, 'message' => '请求成功', 'data' => ''];
    }

    //手机型号
    public function PhoneModels()
    {
        $info = Brand::where('is_show', '1')->where('p_id', '0')->with('subclass')->get()->toArray();

        return ['code' => 1, 'message' => '请求成功', 'data' => $info];
    }

    //免费领膜
    public function GetFreeFilm(Request $request)
    {
        //获取参数
        $u_id = $request->u_id;
        $phone_model = $request->post('phone_model');
        $serial_number = $request->post('serial_number');
        $phone = $request->post('phone');
        $code = $request->post('code');

        $info = DB::table('brought_membranes')->where('u_id', $u_id)->orderBy('create_at', 'DESC')->first();
        if(!empty($info)) {
            if(time() - strtotime($info->create_at) < 2592000) {
                return ['code' => 0, 'message' => '您一个月内已经申请领过一次手机膜，请下次再领', 'data' => ''];
            }
        }

        //短信验证码判断
        $message = DB::table('messages')->where('mobile', $phone)->where('is_use', '0')->orderBy('send_at', 'desc')->first();
        if(empty($message)) {
            return ['code' => 0, 'message' => '短信验证码已使用', 'data' => ''];
        }
        if(time() > strtotime($message->overdue_at)) {
            return ['code' => 0, 'message' => '短信验证码已过期', 'data' => ''];
        }
        if($message->code != $code) {
            return ['code' => 0, 'message' => '短信验证码不正确', 'data' => ''];
        }

        $provider_id = DB::table('users')->where('serial_number', $serial_number)->whereIn('identity', [2, 3])->value('id');
        if(empty($provider_id)) {
            return ['code' => 0, 'message' => '商家不存在', 'data' => ''];
        }

        //插入数据
        $inst['phone_model'] = $phone_model;
        $inst['provider_id'] = $provider_id;
        $inst['phone'] = $phone;
        $inst['u_id'] = $u_id;
        $inst['create_at'] = date('Y-m-d H:i:s', time());
        $res = DB::table('brought_membranes')->insertGetId($inst);
        if (!$res) {
            return ['code' => 0, 'message' => '请求失败', 'data' => ''];
        }
        DB::table('messages')->where('id', $message->id)->update(['is_use' => 1]);

        return ['code' => 1, 'message' => '领取成功', 'data' => ''];
    }

    //快递列表
    public function LogisticsList()
    {
        $list = DB::table('logistics_companies')->where('is_show', 1)->select('id', 'name')->get();
        return ['code' => 1, 'message' => '请求成功', 'data' => $list];
    }

    //地址列表
    public function addressList()
    {
        $list = Area::where('region_type', '1')->with('list')->get()->toArray();

        $return = [];
        foreach ($list as $k => $v) {
            $res['id'] = $v['id'];
            $res['name'] = $v['region_name'];
            $res['list'] = [];
            foreach ($v['list'] as $kk => $vv) {
                $aaa['id'] = $vv['id'];
                $aaa['name'] = $vv['region_name'];
                $res['list'][] = $aaa;
            }
            $return[] = $res;
        }
        return ['code' => 1, 'message' => '请求成功', 'data' => $return];
    }

    //上传图片
    public function uploadImg(Request $request)
    {
        $image = $request->file('image');

        $url = upload($image, 'shares');

        return ['code' => 1, 'message' => '请求成功', 'data' => $url];
    }

    //上传多张图片
    public function uploadsImg(Request $request)
    {
        $image = $request->file('image');

        $url = uploads($image, 'shares');

        return ['code' => 1, 'message' => '请求成功', 'data' => $url];
    }

    //判断商家是否存在
    public function merchantExist(Request $request) {
        $id = $request->post('id');

        $user = DB::table('users')->where('serial_number', $id)->where('identity', '2')->orWhere('identity', '3')->first();

        if(empty($user)) {
            return 0;
        }else{
            return 1;
        }
    }

    //校验用户
    public function checkUser(Request $request) {
        $u_id = $request->u_id;

        $user = DB::table('users')->where('id', $u_id)->first();
        if($user->is_partner == '0' || $user->is_partner == '1') {
            return ['code' => 0, 'message' => '用户未申请成为合作商或正在审核，不能发布订单', 'data' => ''];
        }

        if(empty($user->alipay_account) || empty($user->alipay_name) || empty($user->bank_name) || empty($user->bank_number) || empty($user->bank_type) || empty($user->opening_bank)) {
            return ['code' => 0, 'message' => '用户收款信息为完善，请先完善收款信息', 'data' => ''];
        }

//        if(empty($user->partner_name) || empty($user->partner_phone) || empty($user->partner_address)) {
//            return ['code' => 0, 'message' => '用户地址信息为完善，请先完善地址信息', 'data' => ''];
//        }

        return ['code' => 1, 'message' => '用户信息已完善', 'data' => ''];
    }

    //活动图片
    public function activityShow(Request $request) {
        $type = $request->post('type');

        $picture = DB::table('shows')->where('type', $type)->value('picture');

        if(empty($picture)) {
            return ['code' => 0, 'message' => '图片不存在', 'data' => ''];
        }
        return ['code' => 1, 'message' => '请求成功', 'data' => $picture];
    }
}
